package com.rl.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.rl.domain.*;
import com.rl.service.UserService;
import com.rl.util.TimeFile;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/user")
public class UserController {
    static String NAME = "1";
    @Resource
    private UserService service;

    @RequestMapping("/payment.do")
    @ResponseBody
    public boolean payment(){
        System.out.println("payment");
        boolean flag = false;
        int i = TimeFile.readFile();
        if (i>0){
            flag=true;
            System.out.println(flag);
            TimeFile.writeFile(0);
        }
        return flag;
    }
    @RequestMapping("/payment2.do")
    @ResponseBody
    public boolean payment2(String pwd){
        TimeFile.writeFile(1);
        int i = TimeFile.readFile();
        User user = service.login(NAME, pwd);
        System.out.println("payment2");
        boolean flag = false;
        if (user!=null&&i>0){
            System.out.println("支付完成");
            flag=true;
        }
        return flag;
    }
    @RequestMapping("/login.do")
    @ResponseBody
    public Map<Object,Object> login(String name, String pwd) throws JsonProcessingException {
        System.out.println("进入login");
        Map map = new HashMap<String,Object>();
        String msg=" ";
        boolean success = false;
        User user = service.login(name, pwd);
        if (user==null){
            msg = "用户名或密码错误";
            success = false;
        }else if (user.getFlag()==1) {
            NAME=name;
            success = true;
        }else if (user.getFlag()==2) {
            NAME=name;
            success = true;
        }else {
            msg = "登录中...";
            NAME=name;
            success = true;
        }
        map.put("success",success);
        map.put("msg",msg);
        if (user!=null){
            map.put("name",user.getUname());
            map.put("flag",user.getFlag());
        }
        return map;
    }
    @Transactional
    @RequestMapping("/addUser.do")
    @ResponseBody
    public Map<Object,Object>addUser(String name, String pwd){
        System.out.println("进入addUser");
        Map map2 = new HashMap<String,Object>();
        String msg=" ";
        boolean success = false;
        User user = service.login(name, pwd);
        if(user!=null){
            msg = "用户名重复，请重新填写";
        }else {
            user = new User();
            user.setUname(name);
            user.setPwd(pwd);
            user.setFlag(1);
            service.insertUser(user);
            msg = "注册成功...";
            success = true;
        }
        map2.put("success",success);
        map2.put("msg",msg);
        return map2;
    }
    @RequestMapping("/selectBooks.do")
    @ResponseBody
    public List<Book> selectBooks(){
        System.out.println("进入selectBooks");
        List<Book> books = service.selectBook();
        return books;
    }
    @RequestMapping("/selectBookById.do")
    @ResponseBody
    public Book selectBookById(int bid){
        System.out.println("selectBookById");
        Book book = service.selectBookById(bid);
        return book;
    }

    @RequestMapping("/insertShoppingCart.do")
    @ResponseBody
    public void insertShoppingCart(String bname,String uname,int bnum){
        System.out.println("insertShoppingCart");
        int bid = service.selectBookIdByName(bname);
        int uid = service.selectUserIdByName(uname);
        ShoppingCart shoppingCart = new ShoppingCart();
        shoppingCart.setBid(bid);
        shoppingCart.setUid(uid);
        shoppingCart.setBnum(bnum);
        service.insertShoppingCart(shoppingCart);
    }
    @RequestMapping("/selectBooksByName.do")
    @ResponseBody
    public List<Book> selectBooksByName(String name){
        System.out.println("selectBooksByName");
        List<Book> books = service.selectBooksByName(name);
        return books;
    }
    @RequestMapping("/selectShoppingCartByUserName.do")
    @ResponseBody
    public List<Book> selectShoppingCartByUserName(String name){
        System.out.println("selectShoppingCartByUserName");
        List<Book> books = service.selectShoppingCarByUserName(name);
        return books;
    }
    @RequestMapping("/selectShoppingCartBookByBidUname.do")
    @ResponseBody
    public Book selectShoppingCartBookByBidUname(int bid,String name){
        System.out.println("selectShoppingCartBookByBidUname");
        Book book = service.selectShoppingCartBookByBidUname(name,bid);
        return book;
    }
    @RequestMapping("/updataShoppingcartBookByBnameUname.do")
    @ResponseBody
    public boolean updataShoppingcartBookByBnameUname(String bname, String name, int bnum){
        System.out.println("updataShoppingcartBookByBnameUname");
        int bid = service.selectBookIdByName(bname);
        int uid = service.selectUserIdByName(name);
        if (bnum==0){
            service.deleteShoppingcartBookByBidUid(bid,uid);
        }else {
            service.updataShoppingcartBookByBidUid(bnum,bid,uid);
        }
        return true;
    }
    @RequestMapping("/paymentBookByIdUname.do")
    @ResponseBody
    public boolean paymentBookByIdUname(Integer[] bids, String name){
        System.out.println("paymentBookByIdUname");
        ShoppingHistory shoppingHistory = new ShoppingHistory();
        Orders orders = new Orders();
        orders.setFlag(0);
        orders.setUid1(4);
        int uid = service.selectUserIdByName(name);
        orders.setUid2(uid);
        shoppingHistory.setUid(uid);
        for (int i = 0; i < bids.length; i++) {
            Book book = service.selectShoppingCartBookByBidUname(name,bids[i]);
            shoppingHistory.setBid(bids[i]);
            shoppingHistory.setBnum(book.getNum());
            orders.setBid(bids[i]);
            orders.setBnum(book.getNum());
            service.insertOrders(orders);
            service.insertShoppingHistory(shoppingHistory);
            service.deleteShoppingcartBookByBidUid(bids[i],uid);
        }
        return true;
    }
    @RequestMapping("/selectShoppingHistoryUserName.do")
    @ResponseBody
    public List<Book> selectShoppingHistoryUserName(String name){
        System.out.println("selectShoppingHistoryUserName");
        List<Book> books = service.selectShoppingHistoryUserName(name);
        return books;
    }
    @RequestMapping("/deleteHistoryByUnameBid.do")
    @ResponseBody
    public boolean deleteHistoryByUnameBid(String[] bnames, String name){
        System.out.println("deleteHistoryByUnameBid");
        int uid = service.selectUserIdByName(name);
        for (int i = 0; i < bnames.length; i++) {
            int bid = service.selectBookIdByName(bnames[i]);
            service.deleteHistoryByUidBid(bid,uid);
        }
        return true;
    }
    @RequestMapping("/deleteshoppingcart.do")
    @ResponseBody
    public boolean deleteshoppingcart(int[] bids, String name){
        System.out.println("deleteshoppingcart");
        int uid = service.selectUserIdByName(name);
        for (int i = 0; i < bids.length; i++) {
            service.deleteShoppingcartBookByBidUid(bids[i],uid);
        }
        return true;
    }
    @RequestMapping("/gengaixinxi.do")
    @ResponseBody
    public boolean gengaixinxi(String newname, String name,String pwd){
        System.out.println("gengaixinxi");
        System.out.println(newname);
        System.out.println(name);
        System.out.println(pwd);
        User user = service.selectUserByName(name);
        System.out.println(user);
        if (pwd.equals("******")){
            pwd = user.getPwd();
        }
        service.updataUserByName(newname,pwd,name);
        return true;
    }
    @RequestMapping("/selectEmailByName.do")
    @ResponseBody
    public List<Mail> selectEmailByName(String name){
        System.out.println("selectEmailByName");
        List<Mail> mails = service.selectEmailByName(name);
        return mails;
    }
    @RequestMapping("/selectOrdersByUid.do")
    @ResponseBody
    public List<Orders> selectOrdersByUid(String name){
        System.out.println("selectOrdersByUid");
        int uid1 = service.selectUserIdByName(name);
        List<Orders> orders = service.selectOrdersByUid(uid1);
        return orders;
    }
    @RequestMapping("/selectFaOrdersByUid.do")
    @ResponseBody
    public List<Orders> selectFaOrdersByUid(String name){
        System.out.println("selectFaOrdersByUid");
        int uid1 = service.selectUserIdByName(name);
        List<Orders> orders = service.selectFaOrdersByUid(uid1);
        return orders;
    }
    @RequestMapping("/selectByOrdersByUid.do")
    @ResponseBody
    public List<Orders> selectByOrdersByUid(String name){
        System.out.println("selectByOrdersByUid");
        int uid1 = service.selectUserIdByName(name);
        List<Orders> orders = service.selectByOrdersByUid(uid1);
        return orders;
    }
    @RequestMapping("/selectOrdersByUid1.do")
    @ResponseBody
    public List<Orders> selectOrdersByUid1(String name){
        System.out.println("selectOrdersByUid1");
        int uid1 = service.selectUserIdByName(name);
        List<Orders> orders = service.selectOrdersByUid1(uid1);
        return orders;
    }
    @RequestMapping("/jieshou.do")
    @ResponseBody
    public boolean jieshou(Integer oid){
        if (oid==null)return true;
        System.out.println("jieshou");
        service.updateOrdersByOid(1 ,oid);
        Orders orders = service.selectOrdersByOid(oid);
        Book book = service.selectBookById(orders.getBid());
        User user1 = service.selectUserByUid(orders.getUid1());
        User user2 = service.selectUserByUid(orders.getUid2());
        if (user1==null||user2==null){
            System.out.println("为空");
        }
        Mail mail = new Mail();
        mail.setTitle("已发送");
        mail.setText(book.getBname());
        mail.setUname1(user1.getUname());
        mail.setUname2(user2.getUname());
        service.insertEmail(mail);
        return true;
    }
    @RequestMapping("/fasong.do")
    @ResponseBody
    public boolean fasong(int oid){
        System.out.println("fasong");
        service.updateOrdersByOid(2 ,oid);
        service.updateOrdersUid1ByOid(3,oid);
        return true;
    }
    @RequestMapping("/paisong.do")
    @ResponseBody
    public boolean paisong(int oid){
        System.out.println("paisong");
        Orders orders = service.selectOrdersByOid(oid);
        if (orders.getFlag2()==1)return true;
        service.updateOrdersFlag2ByOid(1,oid);
        int i = orders.getBnum();
        Book book = service.selectBookById(orders.getBid());
        i = book.getNum()-i;
        if (i>=0){
            service.updateBookNum(orders.getBid(),i);
            //删除订单
        }
        return true;
    }
    @RequestMapping("/kucun.do")
    @ResponseBody
    public List<Book> kucun(){
        System.out.println("kucun");
        List<Book>books = service.selectBook();
        return books;
    }

}
